from django.contrib import admin
from django.forms import TextInput, Textarea

from .models import *


@admin.action(description='Заблокировать пользователей')
def ban_users(request, queryset):
    queryset.update(status=TgUser.BANNED)


class CompoundInline(admin.StackedInline):
    model = Compound
    extra = 0


class UserLogsInline(admin.StackedInline):
    model = LogTgUser
    extra = 0


class BonusHistoryInline(admin.StackedInline):
    model = BonusHistory
    extra = 0


class VoucherHistoryInline(admin.StackedInline):
    model = VoucherHistory
    extra = 0


class ProductInline(admin.StackedInline):
    model = Product
    extra = 0
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size': '20'})},
        models.TextField: {'widget': Textarea(attrs={'rows': 4, 'cols': 40})},
    }


class SalesInline(admin.StackedInline):
    model = Sales
    extra = 0
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size': '20'})},
        models.TextField: {'widget': Textarea(attrs={'rows': 4, 'cols': 40})},
    }
    autocomplete_fields = ['user', 'team', 'product']


@admin.register(TgUser)
class TgUserAdmin(admin.ModelAdmin):
    model = TgUser
    list_display = ['user_id', 'date_create', 'username', 'name', 'phone_number',
                    'invite_by', 'balance', 'warns', 'total_sum', 'status',
                    ]
    list_filter = ('status', 'date_create', 'warns', 'total_sum', 'balance')
    search_fields = ('name', 'phone_number', 'user_id')
    ordering = ['date_create', 'status', 'balance']
    actions = [ban_users]
    inlines = [UserLogsInline]


@admin.register(CashBack)
class CashbackAdmin(admin.ModelAdmin):
    list_display = ['amount', 'discount']


@admin.register(Action)
class ActionAdmin(admin.ModelAdmin):
    list_display = ['name']


@admin.register(Config)
class ConfigAdmin(admin.ModelAdmin):
    model = Config
    readonly_fields = ['bot_id', 'bot_url']


@admin.register(Seat)
class SeatAdmin(admin.ModelAdmin):
    list_display = ['name', 'max_count', 'status', 'join']
    search_fields = ['name', 'max_count']


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'is_visible']
    inlines = [ProductInline]


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'price', 'is_visible']
    inlines = [CompoundInline]
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size': '20'})},
        models.TextField: {'widget': Textarea(attrs={'rows': 4, 'cols': 40})},
    }
    search_fields = ['name']


@admin.register(Consumable)
class ConsumableAdmin(admin.ModelAdmin):
    model = Consumable
    list_display = ['name', 'price', 'unit', 'total']
    search_fields = ('name',)
    actions = [ban_users]


@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
    list_display = ['name', 'is_active', 'get_sales_count']
    Team.get_sales_count.short_description = 'Всего заказов'
    search_fields = ['name']


@admin.register(LogTgUser)
class LogTgUserAdmin(admin.ModelAdmin):
    fields = ['user', 'change_type', 'changed_from',
              'changed_to', 'comment']
    list_display = ['user', 'get_change_type_display', 'comment', 'date_create', 'date_update']
    readonly_fields = ['user', 'change_type', 'changed_from', 'changed_to']

    def get_change_type_display(self, obj):
        return obj.get_change_type_display()

    get_change_type_display.short_description = 'Тип изменения'
    get_change_type_display.admin_order_field = 'change_type'


@admin.register(Bonus)
class BonusAdmin(admin.ModelAdmin):
    list_display = ['promo', 'product_id', 'count_left', 'discount', 'end_date']
    inlines = [BonusHistoryInline]


@admin.register(Voucher)
class BonusAdmin(admin.ModelAdmin):
    list_display = ['voucher', 'voucher_sum', 'count_left', 'end_date']

    inlines = [VoucherHistoryInline]


@admin.register(SalesMain)
class SalesMainAdmin(admin.ModelAdmin):
    fields = ['seat', 'start_date', 'end_date', 'comment']
    list_display = ['seat', 'start_date', 'end_date']
    inlines = [SalesInline,]
